package com.yipao.db.service;

import com.yipao.db.constant.DbConstant;
import com.yipao.db.dao.PermissionMapper;
import com.yipao.db.domain.YipaoPermission;
import com.yipao.db.domain.YipaoPermissionExample;
import com.yipao.db.util.DeleteStatusConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 权限
 *
 * @author tianhao
 */
@Service
public class PermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<String>();
        if (roleIds.length == 0) {
            return permissions;
        }

        YipaoPermissionExample example = new YipaoPermissionExample();
        example.or().andRoleIdIn(Arrays.asList(roleIds)).andDeletedEqualTo(false);
        List<YipaoPermission> permissionList = permissionMapper.selectByExample(example);

        for (YipaoPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }


    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if (roleId == null) {
            return permissions;
        }

        YipaoPermissionExample example = new YipaoPermissionExample();
        example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
        List<YipaoPermission> permissionList = permissionMapper.selectByExample(example);

        for (YipaoPermission permission : permissionList) {
            permissions.add(permission.getPermission());
        }

        return permissions;
    }

    /**
     * 判断是不是超级管理员
     * permission为*,代表超级管理员
     *
     * @param roleId 角色id
     * @return
     */
    public boolean checkSuperPermission(Integer roleId) {
        if (roleId == null) {
            return false;
        }

        YipaoPermissionExample example = new YipaoPermissionExample();
        example.or().andRoleIdEqualTo(roleId).andPermissionEqualTo("*").andDeletedEqualTo(false);
        return permissionMapper.countByExample(example) != 0;
    }

    /**
     * 是否拥有运营端的权限
     *
     * @param roleIds
     * @return
     */
    public boolean checkOperatePermission(Integer[] roleIds) {
        if (roleIds==null || roleIds.length == 0) {
            return false;
        }
        return permissionMapper.queryCountUserPermissionByRoleId(Arrays.asList("*", DbConstant.DEFAULT_OPERATION_API), Arrays.asList(roleIds), DeleteStatusConstant.NOT_DELETED) > 0;
    }

    public void deleteByRoleId(Integer roleId) {
        YipaoPermissionExample example = new YipaoPermissionExample();
        example.or().andRoleIdEqualTo(roleId).andDeletedEqualTo(false);
        permissionMapper.logicalDeleteByExample(example);
    }

    public void add(YipaoPermission YipaoPermission) {
        YipaoPermission.setAddTime(LocalDateTime.now());
        YipaoPermission.setUpdateTime(LocalDateTime.now());
        permissionMapper.insertSelective(YipaoPermission);
    }
}
